import { fileURLToPath, URL } from "node:url";

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueDevTools from "vite-plugin-vue-devtools";

//自动引入配置
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    //自动引入插件
    AutoImport({
      imports: ["vue", "vue-router", "pinia"],
      dts: "src/auto-imports.d.ts",
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    //配置代理
    proxy: {
      //代理入口
      "^/api": {
        //目标地址
        target: "http://127.0.0.1:8080", // Tomcat服务器地址
        //开启跨域
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ""),
        // 保持连接活跃以支持SSE
        ws: true,
        configure: (proxy, _options) => {
          proxy.on("proxyRes", (proxyRes, req, res) => {
            // 设置SSE需要的响应头
            res.setHeader("Content-Type", "text/event-stream");
            res.setHeader("Cache-Control", "no-cache");
            res.setHeader("Connection", "keep-alive");
          });
        },
      },
    },
  },
  optimizeDeps: {
    include: ["element-plus", "vue-router", "pinia"],
  },
});
